<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像盒子 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" />
      </div>
      <!-- 登录表单 -->
      <el-form
        ref="loginRef"
        label-width="0px"
        :model="loginData"
        :rules="loginRules"
        class="login_form"
      >
        <!-- 用户名 文本框 -->
        <el-form-item prop="username">
          <el-input
            prefix-icon="iconfont icon-user"
            v-model="loginData.username"
          ></el-input>
        </el-form-item>
        <!-- 密码框 -->
        <el-form-item prop="password">
          <el-input
            type="password"
            prefix-icon="iconfont icon-3702mima"
            v-model="loginData.password"
          ></el-input>
        </el-form-item>
        <!-- 按钮区 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="success" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>

export default {
  name: 'app',
  data () {
    return {
      // 表单数据对象
      loginData: {
        username: '',
        password: ''
      },
      // 验证规则对象
      loginRules: {
        username: [
          { required: true, message: '请输入登录名', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    login () {
      // 校验是否通过
      this.$refs.loginRef.validate(async isPass => {
        if (!isPass) {
          return
        }
        // 异步提交数据
        const { data: resBody } = await this.$http.post('login', this.loginData)
        // 判断响应结果
        if (resBody.meta.status !== 200) {
          return this.$msg.error(resBody.meta.msg)
        }
        this.$msg.success(resBody.meta.msg)
        // 保存token令牌到内存中
        window.sessionStorage.setItem('token', resBody.data.token)
        // 编程式导航：修改hash值实现SPA的跳转
        this.$router.push('/home')
      })
    },
    // 重置方法
    reset () {
      this.$refs.loginRef.resetFields()
    }
  },
  components: {
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: #acf;
  color: #fff;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 10px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.avatar_box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #fff;
  }
}
.login_form {
  position: absolute;
  bottom: 0px;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: space-between;
}
</style>
